﻿namespace Jsl.FxCop
{
    using System;
    using Microsoft.FxCop.Sdk;

    /// <summary>
    /// Utility methods for UI.
    /// </summary>
    public static class UIHelper
    {
        #region Public Methods
        /// <summary>
        /// Determines whether type is a UI type.
        /// </summary>
        /// <param name="type">The type to test.</param>
        /// <returns><c>true</c> if the type is a UI type.</returns>
        public static bool IsUIType(this TypeNode type)
        {
            return type.IsAssignableTo(WellKnownTypeNames.Form, WellKnownTypeNames.UserControl);
        }

        /// <summary>
        /// Does the action if the <see cref="Member"/> is a <see cref="Field"/> on a UI.
        /// </summary>
        /// <param name="member">The member to check.</param>
        /// <param name="action">The action to do with the field.</param>
        public static void DoIfUIField(this Member member, Action<Field> action)
        {
            var field = member as Field;
            if (field != null)
            {
                if (field.DeclaringType.IsUIType())
                {
                    action(field);
                }
            }
        }
        #endregion Public Methods
    }
}